package com.zanglikun.rocketmq.order;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author: zanglikun
 * @Date: 2022/3/31 19:54
 * @Description: 消费者
 */
public class SeleterConsumer {
    public static void main(String[] args) throws Exception {
        // todo 1 创建消费者，同时指定消费组
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");
        // todo 2 指定NameServer
        consumer.setNamesrvAddr("192.168.253.128:9876;192.168.253.129:9876");
        // todo 3 订阅主题
        consumer.subscribe("OrderTopic","*");
        // todo 4 设置回调函数，以处理消息
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("线程名称:"+Thread.currentThread().getName()+",消费的消息："+ new String(messageExt.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        // todo 5 启动消费者
        consumer.start();
    }
}
